package com.example.common.response;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author: ChenZhiXiang
 * @Date: 2019/6/18 22:04
 * @Description: 返回数据封装类
 */
public class ResponseData {

    private Integer code;

    private String msg;

    private Object data;

    public static final ResponseData FAILURE = new ResponseData(ComResponseEnum.FAILURE);

    public static final ResponseData SUCCESS = new ResponseData(ComResponseEnum.SUCCESS);

    public static final ResponseData OPERATION = new ResponseData(ComResponseEnum.OPERATION);

    /**
     *
     * @param responseEnum
     */
    public ResponseData(IResponseEnum responseEnum){
        this.code = responseEnum.code();
        this.msg = responseEnum.msg();
    }

    /**
     * 异常时还需要数据的情况
     * @param responseEnum
     * @param data
     */
    public ResponseData(IResponseEnum responseEnum, Object data) {
        this.code = responseEnum.code();
        this.msg = responseEnum.msg();
        this.data = data;
    }

    /**
     * 失败消息，但是不回滚事务
     *
     * @param
     */
    public static String FiledResponse(String msg) throws Exception {
        Map<String, Object> resMap = new HashMap<>();
        resMap.put("code", 500);
        resMap.put("msg", msg);
        return new ObjectMapper().writeValueAsString(resMap);
    }

    /**
     * 和Token相关的失败消息
     *
     * @param
     */
    public static String tokenResponse(String msg) throws Exception {
        Map<String, Object> resMap = new HashMap<>();
        resMap.put("code", 2001);
        resMap.put("msg", msg);
        return new ObjectMapper().writeValueAsString(resMap);
    }

    public static String turnResponse(Object obj) throws Exception {
        Map<String,Object> resMap = new HashMap<>();
        if (obj instanceof Pager){
            resMap.put("code",200);
            resMap.put("msg","操作成功");
            resMap.put("totalCount",((Pager) obj).getTotalRecord());
            resMap.put("data",((Pager) obj).getDataList());
        } else {
            resMap.put("code",200);
            resMap.put("msg","操作成功");
            resMap.put("data",obj);
        }
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        return new ObjectMapper().writeValueAsString(resMap);
    }

    public static String comResponse(ComResponseEnum comResponseEnum) throws Exception {
        Map<String,Object> resMap = new HashMap<>();
        resMap.put("code",comResponseEnum.code());
        resMap.put("msg",comResponseEnum.msg());
        return new ObjectMapper().writeValueAsString(resMap);
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }
}
